package com.zhao.controller;

import com.zhao.Others.FileOperationObject;
import com.zhao.pojo.*;
import com.zhao.service.PublicizeService;
import com.zhao.utils.JacksonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Controller
public class PublicityController {

    @Autowired
    private PublicizeService publicizeService;

    @ResponseBody
    @RequestMapping("admin-seeOnePublicize.do")
    public String adminSeeOnePublicize(String publicizeID,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "请先登录";
        }
        String res = publicizeService.getOnePublicize(publicizeID,admin,session);

        return res;
    }

    @RequestMapping("admin-getAllPublicize.do")
    public String adminGetAllPublicize(HttpSession session){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = publicizeService.getAllPublicize(admin.getUniversityID(),session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-allPublicize";
    }

    @ResponseBody
    @RequestMapping("admin-uploadPicture.do")
    public String adminUploadPicture(MultipartFile file,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "请先登录";
        }

        String visitPath = FileOperationObject.savePicture(file,"publicitypicture",admin.getUniversityID(),admin.getAdminID());

        PictureJsonObject pictureJsonObject = new PictureJsonObject(visitPath);

        String json = JacksonUtil.bean2Json(pictureJsonObject);
        System.out.println(json);

        return json;
    }

    @ResponseBody
    @RequestMapping("admin-uploadPublicize.do")
    public String adminUploadPublicize(String title, String text, HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "请先登录";
        }

        Publicize publicize = new Publicize();
        publicize.setTitle(title);
        publicize.setText(text);
        String visitPath = getVisitPathFromHTML(text);
        //System.out.println(text);
        //System.out.println(visitPath);
        int indexOfImg = text.indexOf("img",0);
        if(indexOfImg>-1){
            publicize.setImage(visitPath);
        }else{
            publicize.setImage("publicitypicture/公告.jpg");
        }
        String res = publicizeService.adminUploadPublicize(admin,publicize,session);
        System.out.println(res);

        return res;

    }


    @RequestMapping("getAllPublicize.do")
    public String getAllPublicize(HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        Student student = (Student)session.getAttribute("student");

        if(schoolCounsellor == null && student == null){

            return "redirect:/";

        }else{

            if(schoolCounsellor != null && student == null){

                String res = publicizeService.getAllPublicize(schoolCounsellor.getUniversityID(),session);
                if(!res.equals("success")){
                    return "404";
                }

                return "redirect:/newSchoolCounsellor-showAllPublicize";

            }else {

                String res = publicizeService.getAllPublicize(student.getUniversityID(),session);
                if(!res.equals("success")){
                    return "404";
                }

                return "redirect:/newStudent-showAllPublicize";
            }

        }
    }

    ////////////////// 辅导员 /////////////////////

    @RequestMapping("deletePublicize.do")
    public String deletePublicize(String publicizeID,HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            return "redirect:/";
        }

        String res = publicizeService.deletePublicize(publicizeID,schoolCounsellor,session);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/findAllMyPublicize.do";
    }

    //某一个公告
    @RequestMapping("getOnePublicize.do")
    public String getOnePublicize(String publicizeID,HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        Student student = (Student) session.getAttribute("student");
        Admin admin = (Admin) session.getAttribute("admin");
        Superviser superviser = (Superviser)session.getAttribute("superviser");
        System.out.println(publicizeID);
        if(schoolCounsellor == null && student == null && admin == null &&superviser == null){
            return "redirect:/";
        }
        String res="error";
        if(schoolCounsellor!=null){
            res =publicizeService.getOnePublicize(publicizeID,schoolCounsellor,session);
            System.out.println(res);
            if(!res.equals("success")){
                return "404";
            }

            return "redirect:/new-browsePublicize";
        }
        else if(student!=null){
            res = publicizeService.getOnePublicize(publicizeID,student,session);

            System.out.println(res);
            if(!res.equals("success")){
                return "404";
            }

            return "redirect:/newStudent-browsePublicize";
        }else if(admin!=null){
            res = publicizeService.getOnePublicize(publicizeID,admin,session);

            System.out.println("getone res"+res);
            if(!res.equals("success")){
                return "404";
            }

            return "redirect:/admin-browseOnePublicize";
        }else{
            res = publicizeService.getOnePublicize(publicizeID,superviser,session);

            System.out.println("getone res"+res);
            if(!res.equals("success")){
                return "404";
            }

            return "redirect:/superviser-browseOnePublicize";
        }


    }

    //辅导员查看自己编辑过的公告
    @RequestMapping("findAllMyPublicize.do")
    public String findAllMyPublicize(HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "/";
        }
        String res  = publicizeService.findAllMyPublicize(schoolCounsellor,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/newShow-AllMyPublicity";
    }


    @ResponseBody
    @RequestMapping("uploadPicture.do")
    public String uploadPicture(MultipartFile file,HttpSession session){

        System.out.println("123");
        System.out.println(file.getOriginalFilename());
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "error";
        }

        String visitPath = FileOperationObject.savePicture(file,"publicitypicture",schoolCounsellor.getUniversityID(),schoolCounsellor.getSchoolCounsellorID());

        PictureJsonObject pictureJsonObject = new PictureJsonObject(visitPath);

        String json = JacksonUtil.bean2Json(pictureJsonObject);
        System.out.println(json);

        return json;
    }

    private String getVisitPathFromHTML(String html){

        String visitPath = "";

        int indexOfImg = html.indexOf("img",0);
        //System.out.println("indexofimg "+indexOfImg);
        int indexOfp=html.indexOf("=",indexOfImg);
        //System.out.println("indexofp"+indexOfp);
        int indexOfkong=html.indexOf(" ",indexOfp);
        //System.out.println("indexofkong"+indexOfkong);
        visitPath = html.substring(indexOfp+2,indexOfkong-1);


        return visitPath;
    }

    @ResponseBody
    @RequestMapping("uploadPublicize.do")
    public String uploadPublicize(String title, String text, HttpSession session){

        //System.out.println(title);
        //System.out.println("text");
        //System.out.println(text);
        //System.out.println(getVisitPathFromHTML(text));
        Publicize publicize = new Publicize();
        publicize.setTitle(title);
        publicize.setText(text);
        String visitPath = getVisitPathFromHTML(text);
        int indexOfImg = text.indexOf("img",0);
        if(indexOfImg>-1){
            publicize.setImage(visitPath);
        }else{
            publicize.setImage("publicitypicture/公告.jpg");
        }
        String res = publicizeService.uploadPublicize(publicize,session);
        System.out.println(res);

        if(!res.equals("success")){
            return res;
        }

        return "success";
    }

}
